package com.boarsoft.boar.demo.test;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.boarsoft.boar.demo.order.entity.PocOrder;
import com.boarsoft.boar.demo.order.service.PocOrderService;


//@Ignore
public class OrderTest2 {
	
	private static long testStart = 0L;
	private static long testStart2 = 0L;
	private static long processStart = 0L;
	private static long testEnd = 0L;
	
	private static boolean flag = false;
	
	private FileSystemXmlApplicationContext ctx;
	
	@Before
	public void testBefore() {
		
		System.err.println("==================Test  Begin===========================");
		testStart = System.currentTimeMillis();
		//ctx = new ClassPathXmlApplicationContext("classpath:conf/context.xml");
		ctx =new FileSystemXmlApplicationContext("classpath:conf/context.xml");
		testStart2 = System.currentTimeMillis();
	}
	
	@After
	public void testAfter() {
		
		if(flag) {
			System.err.println("==================Test Success===========================");
		}else {
			System.err.println("==================Test Failure===========================");
		}
		
		testEnd = System.currentTimeMillis();
		System.err.println("==================Test  End===========================");
		System.err.println("==================Start Init Env Cost: "+ (testStart2-testStart) + " ms===========================");
		System.err.println("==================Process Business Cost: "+ (testEnd-processStart) + " ms===========================");
	}
	
	// 测试下单
//	@Test
//	public void PlaceOrderTest() {
//		
//		processStart = System.currentTimeMillis();
//		
//		PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
//		
//		//构建订单数据
//		PocOrder order = new PocOrder();
//		
//		order.setUserId(3);
//		order.setTenantId(12344);
//		order.setProductId(1);
//		order.setProductNum(2);
//		order.setAmount(20);
//		
//		// 下单
//		flag = pocOrderService.place(order);
//		assertTrue(flag);
//		
//	}
	
	@Test
	public void FinishOrderTest() {
		
		processStart = System.currentTimeMillis();
		
		PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
		
		//构建订单数据
		PocOrder order = new PocOrder();
		order.setOrderId(3);
		order.setTenantId(12344);
		
		flag = pocOrderService.finish(order, true);
		assertTrue(flag);
	
	}
	
	@Test
	public void QryTrxMsgTest() {
		
//		processStart = System.currentTimeMillis();
//		
//		PocOrderService pocOrderService = ctx.getBean(PocTrxMessageBizImpl.class);
//		
//		String reloadSql = "select messageId, orderId, tenantId, messageStatus, messageTried, "
//				+ "messageMemo, createTime, modifyTime " + "from PocTrxMessage "
//				+ "where messageStatus = '0' or messageStatus = '1' or messageStatus = '2'";
//
//		List<PocTrxMessage> trxMsgList0 = (List<PocTrxMessage>) trxMessageBiz.listBySql(0, reloadSql, 1, 10);
	
	}

//	// 测试取消订单
////	@Test
//	public void CancelOrderTest() {
//
//		processStart = System.currentTimeMillis();
//
//		PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
//
//		// 构建订单数据
//		PocOrder order = new PocOrder();
//		order.setOrderId(1);
//
//		flag = pocOrderService.cancel(order);
//
//	}
//	
//	// 测试查询订单功能
////	@Test
//	public void QueryOrderTest() {
//		
//		processStart = System.currentTimeMillis();
//		
//		PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
//		
//		
//		//构建查询条件
//		int orderId = 0;
//		int userId = 1;
//		String orderBy = "modify_time desc,create_time desc,order_id desc";
//		int pageNo = 2;
//		int pageSize = 10;
//		
//		PagedResult<PocOrder> orders = pocOrderService.list(orderId, userId, orderBy, pageNo, pageSize);
//		
//		System.out.println("===>total:" + orders.getTotal()+"==list size:"+orders.getList().size());
//		
//		System.out.println("===================Page List Result================================");
//		for(PocOrder order : orders.getList()) {
//			System.out.println(order.toString());
//		}
//		
//		flag = true;
//	}
//	
//	// 测试单线程批量下单
////	@Test
//	public void BatchPlaceOrderTest() throws InterruptedException {
//		
//		processStart = System.currentTimeMillis();
//		
//		PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
//		
//		//构建订单数据
//		
//		for(int  i = 1 ; i <= 500 ; i++) {
//			
//			PocOrder order = new PocOrder();
//			
//			//order.setOrderId(i);
//			order.setUserId(1);
//			order.setTenantId(1);
//			order.setProductId(1);
//			order.setProductNum(3);
//			order.setAmount(30);
//			
//			// 下单
//			pocOrderService.place(order);
//		}
//		
//		// ----单线程500单为5597 ms
//		
//		flag = true;
//		
//	}
//	
//	// 测试多线程批量下单
////	@Test
//	public void BatchTreadPlaceOrderTest() throws InterruptedException {
//
//		processStart = System.currentTimeMillis();
//
//		// 测试并发500
//		ExecutorService threadPools = Executors.newFixedThreadPool(20);
//
//		final CountDownLatch count = new CountDownLatch(500);
//
//		for (int i = 1; i <= 500; i++) {
//
//			Thread t = new Thread(new Runnable() {
//
//				PocOrderService pocOrderService = ctx.getBean(PocOrderService.class);
//
//				@Override
//				public void run() {
//					PocOrder order = new PocOrder();
//					order.setUserId(1);
//					order.setTenantId(1);
//					order.setProductId(1);
//					order.setProductNum(3);
//					order.setAmount(30);
//					pocOrderService.place(order);
//					count.countDown();
//				}
//			});
//
//			threadPools.submit(t);
//		}
//
//		// 等待所有线程执行完
//		while (count.getCount() > 0) {
//			System.out.println("=======================>" + count.getCount());
//			Thread.sleep(1000);
//		}
//
//		// ----500线程一单为6773 ms
//
//		flag = true;
//
//	}


}
